package org.jeecg.common.util;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipUtils {


    public static void zip(File zipFile, List<File> fileList) throws Exception  {
        //创建zip输出流
        try (ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile))) {
            //声明文件集合用于存放文件
            byte[] buffer = new byte[1024];
            //将文件放入zip压缩包
            for (int i = 0; i < fileList.size(); i++) {
                if (fileList.get(i).exists()){
                    File file = fileList.get(i);
                    try (FileInputStream fis = new FileInputStream(file)) {
                        out.putNextEntry(new ZipEntry(file.getName()));
                        int len;
                        // 读入需要下载的文件的内容，打包到zip文件
                        while ((len = fis.read(buffer)) > 0) {
                            out.write(buffer, 0, len);
                        }
                        out.closeEntry();
                    }
                }
            }
        }
    }


    public static void downLoadTempZipFile(HttpServletResponse response,String basePath, String zipFileName, List<File> fileList )throws Exception {
        String zipFilePath = basePath + zipFileName;
        File zipFile = new File(zipFilePath);
        try{
            zip(zipFile,fileList);
            downLoadFile(response, zipFile , zipFileName);
        }catch (Exception e){
            throw e;
        }finally {
            if (zipFile.exists()) {
                zipFile.delete();
            }
        }
    }

    public static void downLoadFile(HttpServletResponse response,File zipFile, String fileName) throws Exception {
        try (InputStream ins = new FileInputStream(zipFile);
             BufferedInputStream bins = new BufferedInputStream(ins);
             OutputStream outs = response.getOutputStream();
             BufferedOutputStream bouts = new BufferedOutputStream(outs)) {
//            response.setContentType("application/x-download");
            response.setContentType("application/zip");
            response.setHeader("Access-Control-Expose-Headers", "content-disposition");
            response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
            int bytesRead = 0;
            byte[] buffer = new byte[1024];
            while ((bytesRead = bins.read(buffer, 0, 1024)) != -1) {
                bouts.write(buffer, 0, bytesRead);
            }
            bouts.flush();
        }

    }
    public static void downLoadFile(HttpServletResponse response,File file, String fileName , String contentType) throws Exception {
        try (InputStream ins = new FileInputStream(file);
             BufferedInputStream bins = new BufferedInputStream(ins);
             OutputStream outs = response.getOutputStream();
             BufferedOutputStream bouts = new BufferedOutputStream(outs)) {
//            response.setContentType("application/x-download");
            response.setContentType(contentType);
            response.setHeader("Access-Control-Expose-Headers", "content-disposition");
            response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
            int bytesRead = 0;
            byte[] buffer = new byte[1024];
            while ((bytesRead = bins.read(buffer, 0, 1024)) != -1) {
                bouts.write(buffer, 0, bytesRead);
            }
            bouts.flush();
        }
    }

    public static void main(String[] args) throws Exception {
        List<File> files = new ArrayList<>();
        files.add(new File("d:\\1.txt"));
        files.add(new File("d:\\2.txt"));

        File zipFile =  new File("d:\\3.zip");
        zip(zipFile,files);
    }
}
